package com.example.controller;
import com.example.pojo.Personal;
import com.example.service.PersonalService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
 * Created by IntelliJ IDEA2021.3
 *
 * @Author: Tenghw
 * @Date: 2022/03/21  16:37
 * @Description:
 */
@Slf4j
@Controller
@SuppressWarnings("all")
public class ProviderHystrixController {
   @Autowired
    PersonalService personalService;

    @GetMapping(path = "/getAll")
    @ResponseBody
    public List<Personal> getAllPersonal(){
        log.info("=======进入controller层getAllPersonal()============");
        List<Personal> personals = personalService.getServiceAll();
        return personals;
    }


    @GetMapping(path = "/getOne/{id}")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "errorCallBack")
    /**@HystrixCommand模仿没有数据时，服务熔断。
    若这个方法没有异常且没有超时(hystrix默认1秒算超时)的情况下，才正常返回，否则进入errorCallBack()方法，
    注意：getAllPersonal（）和errorCallBack（）一定要返回类型相同。
     */
    public Personal getOne(@PathVariable("id")int id){
        log.info("=======进入controller层getOne()============");
        Personal personal = personalService.getServiceOne(id);
        if(personal==null){
            throw new RuntimeException("不好意思，服务端已熔断并关闭，您无法继续访问，请稍后再试"); // 服务熔断操作
        }
        return personal;
    }
    public Personal errorCallBack(@PathVariable("id")int id){
        log.info("==========进入controller层的errorCallBack()=============");
        Personal personal = new Personal();
        personal.setId(8888);
        personal.setName("——服务熔断，不存在该用户，或者信息无法查询------"+id);
        personal.setCamp("null");
        personal.setRemarks("no this database in MySQL");
        return  personal;
    }

}
